package com.hitrust.gs.action;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.upload.FormFile;

import system.LoginContext;

import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.business.GSProviderType;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.dao.GSProviderDAO;
import com.hitrust.gs.dao.GsProviderTypeDao;
import com.hitrust.gs.form.ProviderImportForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.db.DBConnectionManager;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;

import framework.core.DBConnection;
import framework.core.DateUtil;

public class GsProviderScanQueryImportAction extends GSBaseAction {

	private static final String IMPORT_RESULT = "importResult";
	public static final String TEMP_PROVIDER = "manufactureExcel";

	public String execute(GSBaseForm aForm) throws Exception {
		ProviderImportForm providerImportForm = (ProviderImportForm) aForm;
	    FormFile formFile = providerImportForm.getExcelfile();
	        
	    HSSFWorkbook workbook = new HSSFWorkbook(formFile.getInputStream());;
	    
	    ArrayList providerTypeList = new ArrayList();
	    
		int numOfSheets = workbook.getNumberOfSheets();
		String importResult = null;
		boolean importSucess = true;
		Connection con = DBConnectionManager.getInstance().getConnection(); 

		try{
			GSProviderDAO providerDao = new GSProviderDAO();
			LoginContext context = (LoginContext) providerImportForm.getSession().getAttribute(GSBaseConstants.LOGIN);
			GsProviderTypeDao gsProviderTypeDao = new GsProviderTypeDao();
			GSProductTypeDao productTypeDao = new GSProductTypeDao();
			for(int i = 0; i < numOfSheets; i++){
				HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(i);
				for(int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++){
					HSSFRow rowline = sheet.getRow(rowNum);
					if(rowline == null){
						continue;
					}
					GSProviderType providerType = new GSProviderType();
					HSSFCell providerCodeCell = rowline.getCell((short)0);
					
					String providerCode = null;
					if(providerCodeCell.getCellType() == HSSFCell.CELL_TYPE_STRING){
						providerCode = providerCodeCell.getStringCellValue();
					}else if(providerCodeCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
						providerCode = (int)providerCodeCell.getNumericCellValue() + "";
					}
					if(providerCode == null){
						continue;
					}
					
					int status = 1;
					HSSFCell statusCell = rowline.getCell((short)2);
					if(statusCell.getCellType() == HSSFCell.CELL_TYPE_STRING){
						status = Integer.parseInt(statusCell.getStringCellValue());
					}else if(statusCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
						status = (int)statusCell.getNumericCellValue();
					}
					
					providerType.setProviderCode(providerCode);
					HSSFCell providerNameCell = rowline.getCell((short)1);
					String providerName =providerNameCell.getStringCellValue();
					providerType.setProviderName(providerName);
					
					providerTypeList.add(providerType);
					
					GSProviderBean provider = providerDao.queryProvider(providerCode);
					if(provider != null ){
						
						providerType.setCreateTime(new DateUtil().getFullString());
						providerType.setCreateUser(context.getLoginUserName());
						providerType.setCreateUserId(context.getLoginUserId());
						providerType.setLastUpdateTime(new DateUtil().getFullString());
						providerType.setLastUpdateUser(context.getLoginUserName());
						providerType.setLastUpdateUserId(context.getLoginUserId());
						providerType.setProviderId(provider.getId());
						providerType.setStatus(new Integer(status));
						if(gsProviderTypeDao.find(providerCode) != null){
							//gsProviderTypeDao.update(providerType);
							if(status == 1){
								productTypeDao.insetByProviderCode(provider.getGs_provider_id(), con, context, GSBaseConstants.PRODUCT_TYPE4);	
								gsProviderTypeDao.setAvailability(con, providerCode);
							}else{
								productTypeDao.deleteByStatus(providerCode, con, "");
								gsProviderTypeDao.delete(con, providerCode);
							}
						}else{
							if(status == 1){
								providerDao.insertProvierAuditType(con,provider,context);
								productTypeDao.insetByProviderCode(provider.getGs_provider_id(), con, context, GSBaseConstants.PRODUCT_TYPE4);
								gsProviderTypeDao.setAvailability(con, providerCode);
							}
						}
					}else{
						importResult = "导入失败，不存在的供应商编码 ：" + providerCode;
  						throw new GSException("导入失败，不存在的供应商编码 ：" + providerCode);
  						
					}
				}
			}
			con.commit();
		}catch(GSException t){
			importSucess = false;
			t.printStackTrace();
			con.rollback();
		}
		catch(Throwable e){
			importResult = "数据库连接异常";
			e.printStackTrace();
			importSucess = false;
			con.rollback();
		}finally{
			 con.close();

		}
		
		if(importSucess){
			aForm.getRequest().setAttribute(IMPORT_RESULT, "1");
		}else{
			aForm.getRequest().setAttribute(IMPORT_RESULT, importResult);
		}
		ResultProcess tProc = new ResultProcess();
        tProc.setResult(providerTypeList);
        
		
		return GSBaseConstants.FORWARD_SUCCESS;
	}

}
